import com.google.appengine.api.datastore.Entity

def s = application['appconfig.lastTranscationShowingLimit']
if(s == null) {
	def entities = datastore.execute{
		from AppConfigE
		where id == lastTranscationShowingLimit
	}
	if(entities.size() > 0) {
		Entity appconfig = entities[0]
		application['appconfig.lastTranscationShowingLimit'] = appconfig.value
	}
}
int flimit = application['appconfig.lastTranscationShowingLimit'] as int

def user = session.user

def tentities = datastore.execute {
	from TranscationE
	sort desc by updatedTime
	where userId == user.id
	limit flimit
}
request.transcations = tentities

def transcationTags = [:]
tentities.each { transcation ->
	def ttentities = datastore.execute {
		from TranscationTagE
		sort asc by name
		where transcationId == transcation.key.id and userId == session.user.id
	}
	def tags = new StringBuilder()
	ttentities.each { tt ->   tags += "<a class=\"table-1-link\" href=\"transcationViewForTag.groovy?transcationTag=$tt.name\">#$tt.name</a> " }
	transcationTags[transcation.key.id] = tags
}
request.transcationTags = transcationTags

use(groovy.time.TimeCategory) {
	int timeZoneHour = session.userTimeZoneHour as int
	int timeZoneMinute = session.userTimeZoneMinute as int
	def now = new Date() + timeZoneHour.hours + timeZoneMinute.minutes
	request.createdDateStg = now.format('dd-MM-yyyy')
	request.createdTimeStg = now.format('HH:mm')
}
request.title = 'Expenses'
request.view = 'transcationJQuery.gsp'

forward 'wydLayout.gsp'
